package com.shanghaionstar.carmanage.adapter;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.shanghaionstar.R;
import com.shanghaionstar.carmanage.ChargingPileActivity;
import com.shanghaionstar.carmanage.model.ChargePileStationEntity;

import java.util.List;

/**
 * User: jun chen
 * Class:　ChargePileStationListAdapter
 * Date: 2016-06-14  10:54
 * Description:充电桩列表适配器
 */
public class ChargePileStationListAdapter extends CommonAdapter<ChargePileStationEntity> {

    private ChargingPileActivity activity;
    private List<ChargePileStationEntity> datas;
    private ChargePileStationEntity chargePileStationEntity;

    private String brand;


    public ChargePileStationListAdapter(List<ChargePileStationEntity> datas, ChargingPileActivity activity,String...args) {
        super(datas, activity);
        this.datas = datas;
        this.activity = activity;
        if(args != null && args.length > 0)
            this.brand = args[0];
    }

    @Override
    public void getView(int position, CommonViewHolder viewHolder) {
        final ViewHolder holder = (ViewHolder) viewHolder;
        chargePileStationEntity = datas.get(position);

        //站点名称
        holder.station_name.setText(chargePileStationEntity.getStationName().trim());
        //站点类型
        if (TextUtils.isEmpty(brand) && !CommonUtil.isNull(chargePileStationEntity.getStationType())) {
            holder.btn_stationType.setVisibility(View.VISIBLE);
            holder.btn_stationType.setText(chargePileStationEntity.getStationType());
        } else {
            holder.btn_stationType.setVisibility(View.GONE);
        }
        //充电桩供应商
        String supplier = chargePileStationEntity.getSupplier();
        if (!CommonUtil.isNull(supplier)) {
            holder.btn_supplier.setVisibility(View.VISIBLE);
            if(TextUtils.isEmpty(brand))
                holder.btn_supplier.setText(supplier);
            else {
                holder.btn_supplier.setBackgroundResource(R.drawable.btn_charging_pile_item_brand);
                holder.btn_supplier.setTextColor(Color.parseColor("#ffffff"));
                if(supplier.equalsIgnoreCase("BUICK")){
                    holder.btn_supplier.setText(R.string.charging_pile_brand_buick);
                }else if(supplier.equalsIgnoreCase("CADILLAC")){
                    holder.btn_supplier.setText(R.string.charging_pile_brand_cadillac);
                }else{
                    holder.btn_supplier.setText(supplier);
                }
            }
        } else {
            holder.btn_supplier.setVisibility(View.GONE);
        }
        //充电桩计费类型
        if (!CommonUtil.isNull(chargePileStationEntity.getFeeType()) && !context.getString(R.string.poi_not_city_str).equals(chargePileStationEntity.getFeeType())) {
            holder.btn_feeType.setVisibility(View.VISIBLE);
            holder.btn_feeType.setText(chargePileStationEntity.getFeeType());
        } else {
            holder.btn_feeType.setVisibility(View.GONE);
        }
//        //四舍五入，保留两位小数
//        double distance = Double.parseDouble(chargePileStationEntity.getDistance().toString());
//        BigDecimal bigDecimal = new BigDecimal(distance / 1000);
//        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
//        double distance1 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//        String distance2 = df.format(distance1);

        //距离
        holder.text_distance.setText(chargePileStationEntity.getDistance());
        //地址
        holder.tv_address_content.setText(chargePileStationEntity.getAddress());
        //类型
        String stationType = chargePileStationEntity.getStationType();//站点类型
        String quickCharge = chargePileStationEntity.getQuickCharge();//快充数量
        String slowCharge = chargePileStationEntity.getSlowCharge();//
        if (CommonUtil.isNull(quickCharge))
            quickCharge = "0";
        if (CommonUtil.isNull(slowCharge))
            slowCharge = "0";
        holder.tv_type_content.setText(stationType + "/" + quickCharge + "个快充/" + slowCharge + "个慢充");
        //状态
        holder.tv_state_content.setText(chargePileStationEntity.getStatus());

    }

    @Override
    public int layoutId() {
        return R.layout.car_manager_charging_pile_list_item;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById(R.id.title_rl)
        LinearLayout title_rl;
        @FindViewById(R.id.station_name)
        TextView station_name;//站点名称
        @FindViewById(R.id.btn_stationType)
        TextView btn_stationType;//站点类型   黄色
        @FindViewById(R.id.btn_supplier)
        TextView btn_supplier;//站点供应商    蓝色
        @FindViewById(R.id.btn_feeType)
        TextView btn_feeType;//站点计费类型   红色
        @FindViewById(R.id.text_distance)
        TextView text_distance;//站点距离
        @FindViewById(R.id.tv_address_content)
        TextView tv_address_content;//站点地址信息
        @FindViewById(R.id.tv_type_content)
        TextView tv_type_content;//站点类型信息
        @FindViewById(R.id.tv_state_content)
        TextView tv_state_content;//站点状态信息
    }
}